package org.example.springboot137.controller;

import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.example.springboot137.config.MinIoClientConfig;
import org.example.springboot137.entity.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

/**
 * minio控制器
 */
@RestController
@CrossOrigin("*")
public class MinioController  {
    @Autowired
    MinioClient minioClient;
    @Autowired
    MinIoClientConfig minIoClientConfig;

    @GetMapping("minioUpload")
    public ResponseEntity minio() throws Exception{
        String fileName="小星.png";//上传文件名
        String filePath="D:\\魔法物语\\小星.png";//上传路径
        FileInputStream fileInputStream = new FileInputStream(filePath);
        ObjectWriteResponse bjectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
                .bucket("star")
                .object(fileName)
                .stream(fileInputStream, -1L, 10485760L)
                .build());
        return new ResponseEntity().addDate("frilmsg",bjectWriteResponse.object());
    }
    @PostMapping("minio")
    public ResponseEntity minio(@RequestParam("file") MultipartFile file) throws Exception{

        String fileName= UUID.randomUUID() +file.getOriginalFilename();//上传文件名
        InputStream inputStream = file.getInputStream();
        ObjectWriteResponse bjectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
                .bucket("star")
                .object(fileName)
                .stream(inputStream, -1L, 10485760L)
                .build());
        return new ResponseEntity().addDate("frilmsg",bjectWriteResponse.object());
    }
}
